package com.netforce.web.admin.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.netforce.data.source.Connection;
import com.netforce.data.source.DataSource;
import com.netforce.util.io.IoManager;

public interface QueryController
{
  @RequestMapping(value = "/query.do", method = RequestMethod.POST)
  public ModelAndView action(HttpServletRequest aRequest, HttpServletResponse aResponse, String aQuery) throws Exception;

  @Controller
  public class DefaultQueryController implements QueryController
  {
    @Override
    public ModelAndView action(HttpServletRequest aRequest, HttpServletResponse aResponse, @RequestParam("query") String aQuery) throws Exception
    {
      ModelAndView modelAndView = new ModelAndView("/views/query.jsp");
      Connection connection = (Connection)aRequest.getSession().getAttribute("connection");
      aRequest.setAttribute("result", IoManager.toString(connection.httpGet(new URIBuilder("/services/data/v30.0/query").addParameter("q", aQuery).toString())));
      return modelAndView;
    }

    @Autowired
    protected DataSource DataSource;
    @Autowired
    protected IoManager IoManager;
  }
}